﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Menus;
using MediatR;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Menus
{
    public class AddMenuCommandHandler : IRequestHandler<AddMenuCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Menu> menuRep;
        private readonly IMapper mapper;

        public AddMenuCommandHandler(IBaseRepository<Menu> menuRep, IMapper mapper)
        {
            this.menuRep = menuRep;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model=mapper.Map<Menu>(request);
            model.CreateTime=DateTime.Now;
            model.UpdateTime = null;
            var res= menuRep.Add(model).Result;
            if (res>0)
            {
                result.Code=APIEnum.添加成功;
                result.Msg=APIEnum.添加成功.ToString();
                result.data=res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }

            return Task.FromResult(result);
        }
    }
}
